package com.mapapp.flutter_mapapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.persenter.EditPasswordPersenter;
import com.mapapp.flutter_mapapp.persenter.ResetPasswordPersenter;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.ResetPasswordViewInterface;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditPasswordActivity extends ImmersiveBaseActivity implements OnClickListener, ResetPasswordViewInterface {
    private static final String TAG = "EditPasswordActivity";
    @BindView(R.id.iv_edit_password_back)
    ImageView ivEditPasswordBack;
    @BindView(R.id.et_edit_input_old_password)
    EditText etEditInputOldPassword;
    @BindView(R.id.et_edit_input_new_password)
    EditText etEditInputNewPassword;
    @BindView(R.id.et_edit_ensure_new_password)
    EditText etEditEnsureNewPassword;
    @BindView(R.id.btn_edit_ensure)
    Button btnEditEnsure;
    @BindView(R.id.btn_edit_cancel)
    Button btnEditCancel;

    private EditPasswordPersenter mPersenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);
        ButterKnife.bind(this);

        mContext = this;
        init();
        mPersenter = new EditPasswordPersenter(this, this, lifecycleSubject);
    }

    private void init() {
        ivEditPasswordBack.setOnClickListener(this);
        btnEditEnsure.setOnClickListener(this);
        btnEditCancel.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_edit_password_back:
                MyActivityManager.getActivityManager().pop(this);
                break;
            case R.id.btn_edit_ensure:
                reset();
                break;
            case R.id.btn_edit_cancel:
                MyActivityManager.getActivityManager().pop(this);
                break;
        }
    }

    private void reset() {
        String oldPassword = etEditInputOldPassword.getText().toString().trim();
        String inputPassword = etEditInputNewPassword.getText().toString().trim();
        String ensurePassword = etEditEnsureNewPassword.getText().toString().trim();
        if ("".equals(oldPassword)) {
            ToastUtils.showMessage(mContext, "请输入旧密码");
            return;
        }
        if ("".equals(inputPassword)) {
            ToastUtils.showMessage(mContext, "请输入新密码");
            return;
        }
        if ("".equals(ensurePassword)) {
            ToastUtils.showMessage(mContext, "请输入确认新密码");
            return;
        }
        if (!inputPassword.equals(ensurePassword)) {
            ToastUtils.showMessage(mContext, "新密码和确认密码不一致");
            return;
        }
        mPersenter.editPassword(oldPassword,inputPassword);
    }

    @Override
    public void resetSuccess() {
        MyActivityManager.getActivityManager().pop(this);
    }
}
